package com.gjy.leetcode;

/**
 * 1945. 字符串转化后的各位数字之和
 *
 * @author 宫静雨
 * @version 1.0
 * @since 2022-12-15 17:13:13
 */
public class A1945 {

    public static void main(String[] args) {
        A1945 a1945 = new A1945();

        System.out.println(a1945.getLucky("zbax", 2));

    }

    /**
     * a = 97
     */
    public int getLucky(String s, int k) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); ++i) {
            char ch = s.charAt(i);
            sb.append(ch - 'a' + 1);
        }
        String digits = sb.toString();
        for (int i = 1; i <= k && digits.length() > 1; ++i) {
            int sum = 0;
            for (int j = 0; j < digits.length(); ++j) {
                char ch = digits.charAt(j);
                sum += ch - '0';
            }
            digits = Integer.toString(sum);
        }

        return Integer.parseInt(digits);
    }

}
